<?php
class ome_payment_func{
    public function sync_payments($shop_id){
        $shopObj = &app::get('ome')->model('shop');
        $cfgObj = &app::get('ome')->model('payment_cfg');
        $payShopObj = &app::get('ome')->model('payment_shop');
        $shop = $shopObj->dump($shop_id);

        $type = array(
            'taobao' => array(
                'bn' => 'alipaytrad',
                'name' => '支付宝担保交易',
            ),
            'paipai' => array(
                'bn' => 'tenpaytrad',
                'name' => '财付通担保交易',
            ),
        );
        if(isset($shop['shop_type']) && in_array($shop['shop_type'],array_keys($type))){
            $pay_bn = $type[$shop['shop_type']]['bn'];
            $pay_name = $type[$shop['shop_type']]['name'];

            $payment = $cfgObj->dump(array('pay_bn'=>$pay_bn), 'id,custom_name,pay_type');
            if (isset($payment['id']) && $payment['id']){
                $payShop = $payShopObj->dump(array('pay_bn'=>$pay_bn,'shop_id' => $shop_id), 'pay_bn,shop_id');
                if(!isset($payShop['pay_bn']) || !$payShop['pay_bn']){
                    $payShopSdf = array(
                        'pay_bn' => $pay_bn,
                        'shop_id' => $shop_id,
                    );
                    $payShopObj->insert($payShopSdf);
                }
            	return true;
            }

            $cfgSdf = array(
	            'custom_name' => $pay_name,
	            'pay_bn' => $pay_bn,
	            'pay_type' => 'online',
	        );
            $payShopSdf = array(
	            'pay_bn' => $pay_bn,
	        	'shop_id' => $shop_id,
	        );
	        $cfgObj->insert($cfgSdf);
            $payShopObj->insert($payShopSdf);
        }else{
            kernel::single("ome_rpc_request_paymethod")->getPayment($shop_id);
        }
        return true;
    }

    public function del_payments($shop_id){
        $shopObj = &app::get('ome')->model('shop');
        $cfgObj = &app::get('ome')->model('payment_cfg');
        $payShopObj = &app::get('ome')->model('payment_shop');

        $payShops = $payShopObj->getList('*',array('shop_id'=>$shop_id));
        $pay_bn = '';
        foreach($payShops as $payShop){
            $pay_bn = $payShop['pay_bn'];
            if($pay_bn){
                $payShopObj->delete(array('pay_bn'=>$pay_bn,'shop_id'=>$shop_id));
                $payShop = $payShopObj->dump(array('pay_bn'=>$pay_bn), 'pay_bn,shop_id');
                if(!isset($payShop['shop_id']) && !$payment['shop_id']){
                    $cfgObj->delete(array('pay_bn'=>$pay_bn));
                }
            }
        }
        return true;
    }
}